package org.wu.framework.lazy.orm.database.lambda.stream.condition.update;

import org.wu.framework.core.CamelAndUnderLineConverter;
import org.wu.framework.lazy.orm.database.lambda.stream.function.Snippet;
import org.wu.framework.lazy.orm.database.lambda.stream.function.SnippetUtil;
import org.wu.framework.lazy.orm.database.lambda.stream.support.LambdaMeta;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class LambdaSetValueBasicComparison<T> extends AbstractSetValueBasicComparison<T> {

    // 类
    protected Class<T> classT;
    // 类名称
    protected String className;

    /**
     * 获取T 的class
     *
     * @return
     */
    @Override
    public Class<T> getClassT() {
        if (null == classT) {
            Type genericSuperclass = getClass().getGenericSuperclass();
            if (genericSuperclass instanceof ParameterizedType) {
                ParameterizedType superClass = (ParameterizedType) genericSuperclass;
                this.classT = (Class<T>) superClass.getActualTypeArguments()[0];
            } else if (className != null) {
                try {
                    this.classT = (Class<T>) Class.forName(className);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return classT;
    }

    /**
     * 列到字符串 表名.子段名
     *
     * @param row
     * @return 子段名
     */
    @Override
    protected String columnToString(Snippet<T, ?> row) {
        LambdaMeta lambdaMeta = SnippetUtil.extract(row);
        assert lambdaMeta != null;
        final Class<?> tClass = lambdaMeta.instantiatedClass();
        if (null == classT) {
            classT = (Class<T>) tClass;
        }
        final String methodName = lambdaMeta.methodName();
        String fieldName = CamelAndUnderLineConverter.methodToField(methodName);
        return CamelAndUnderLineConverter.humpToLine2(fieldName);
    }
}
